<?php
/*
 * 用户扩充资料类
 */
class User_Member_IM
{
	CONST _CONST_TABLE_NAME_ = 'member_online';

	public function __construct()
	{
	}

	/**
	 * 得到某用户的在线联系方式
	 * @param $uid int 用户ID
	 *
	 * @return	$ims = array(
	 *		'number'    => string,
	 *		'type'      => int 1:旺旺 2:QQ 3:msn 
	 *		'nickname'  => string
	 *		'html'      => string
	 * )
	 */
	public static function im ($uid)
	{
		$im              = self::get_online_info($uid);
		if(empty($im)) {
			return array();
		} else {
			$number   = $im['number'];
			$type     = $im['type'];
			$nickname = $im['nickname'];

			if(!empty($number) && $type == 1)
			{
				$html = '<a class="im-ww" target="_blank" href="http://www.taobao.com/webww/ww.php?ver=3&touid='.rawurlencode($number).'&siteid=cntaobao&status=2&charset=utf-8"><img border="0" src="http://amos.alicdn.com/realonline.aw?v=2&uid='.rawurlencode($number).'&site=cntaobao&s=2&charset=utf-8" alt="有事点这里" />'.$nickname.'</a>';
			}
			elseif(!empty($number) && $type == 2)
			{
				$html = '<a class="im-qq"  href="http://wpa.qq.com/msgrd?v=3&uin='.$number.'&site=qq&menu=yes" target="_blank"><img border="0" src="http://wpa.qq.com/pa?p=2:'.$number.':45" alt="联系我" title="联系我">'.$nickname.'</a>';
			}
			elseif(!empty($number) && $type == 3)
			{
				$html = '<a class="im-msn" target="_blank" href="http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee='.rawurlencode($number).'&mkt=zh-CN"><img style="border-style: none;" src="http://messenger.services.live.com/users/'.rawurlencode($number).'/presenceimage?mkt=zh-CN" width="16" height="16" />'.$nickname.'</a>';
			}

			$ims['number']   = $number; 
			$ims['type']     = $type; 
			$ims['nickname'] = $nickname; 
			$ims['html']     = $html;
			return $ims;
		}
	}

	/**
	 * 更新一条在线联系方式 如 不存在，即增加
	 * @param $number 前台用户的在线联系方式号码
	 * @param $type 联系方式的类型 1：旺旺 2：QQ 3：MSN
	 * @param $nickname 联系方式昵称
	 *
	 * @return insert_id 返回插入的id号；判断是否插入成功；
	 */
	public function set_online_info($number = "", $type = 1, $nickname = "")
	{
		if ($this->has_member_online(User::current_user_id())) {
			if($this->update_online_info($number, $type, $nickname)) {
				return true;
			} else {
				return false;
			}
		} else {
			if($this->add_online_info($number, $type, $nickname)) {
				return true;
			} else {
				return false;
			}

		}
	}

	protected static function get_online_info($uid)
	{
		$query = DB::select ("*")
			->from (self::_CONST_TABLE_NAME_)
			->where ('uid', '=', $uid)
			->execute ();

		if ($query->valid())
			return $query->current();
		else
			return NULL;
	}

	/**
	 * 增加一条数据
	 */
	protected function add_online_info($number = "", $type = 1, $nickname = "")
	{
		if(empty($number))
			return false;

		$insert = array();
		$insert['uid']         = User::current_user_id();
		$insert['create_time'] = DB::expr('unix_timestamp(now())');
		$insert['number']      = trim($number);
		$insert['type']        = $type;
		$insert['nickname']    = trim($nickname);

		list($insert_id,$rows) = DB::insert (self::_CONST_TABLE_NAME_)
			->columns (array_keys($insert))
			->values (array_values($insert))
			->execute ();

		return $insert_id;
	}

	protected function update_online_info($number = "", $type = 1, $nickname = "")
	{
		$update = array();
		$update['update_time'] = DB::expr('unix_timestamp(now())');
		$update['number'] = trim($number);
		$update['type'] = $type;
		$update['nickname'] = trim($nickname);
		$query = DB::update (self::_CONST_TABLE_NAME_)
			->set ($update)
			->where ('uid', '=', User::current_user_id())
			->execute ();

		return $query;
	}

	protected function has_member_online($uid)
	{
		$uid = intval($uid);
		$query = DB::select (array('count("id")','c'))
			->from (self::_CONST_TABLE_NAME_)
			->where ('uid', '=', $uid)
			->execute()
			->get('c');
		
		if(intval($query)>0)
			return TRUE;
		else
			return FALSE;
	}
}
